package com.fsp.service;

import com.fsp.domain.Ebook;
import com.fsp.domain.EbookExample;
import com.fsp.mapper.EbookMapper;
import com.fsp.req.EbookReq;
import com.fsp.resp.EbookResp;
import com.fsp.util.CopyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fushengping
 * @className EbookService
 * @description
 * @date 2021/7/26 21:40
 */
@Service
public class EbookService {
    @Resource
    //@Autowired(required = false)
    private EbookMapper ebookMapper;

    //使用模糊查询
    public List<EbookResp> list(EbookReq ebookReq){
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        /*上面两步相当于sql中的where判断*/
        criteria.andNameLike("%" + ebookReq.getName() + "%");//进行参数的模糊查询
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);

        /*将ebookList中的值遍历取出，放入新数组中*/
        //ArrayList<EbookResp> respList = new ArrayList<>();
        /*for (Ebook ebook : ebookList) {
            //EbookResp ebookResp = new EbookResp();
            //*使用BeanUtils的拷贝将原数组中的对象拷入新数组对象中去*//*
            //BeanUtils.copyProperties(ebook,ebookResp);
            *//*使用封装的工具类CopyUtil效果等同前面注释的两行*//*
            EbookResp ebookResp = CopyUtil.copy(ebook, EbookResp.class);
            respList.add(ebookResp);
        }
        return respList;*/
        //列表拷贝：效果等同于前面列表的循环再单独拷贝
        List<EbookResp> list = CopyUtil.copyList(ebookList, EbookResp.class);
        return list;
    }
}
